<?php

namespace app\api\controller\v48;

use app\api\controller\v2\Auth;
use app\common\cachemodel\AppCompensationGiveRecord;
use app\common\cachemodel\UserCoupon;
use app\common\cachemodel\ZlCouponAttr;
use app\common\lib\Util;
use app\zyhsapi\model\ZyhsConsultRtcUserBenefits;
use think\Cache;
use think\response\Json;

class AppCompensation extends Auth
{
    /**
     * 服务异常 补偿领取
     * @return Json
     * @throws \Exception
     */
    public function give()
    {
        $endTime = strtotime('2024-07-30 00:00:00');

        $nowTime = $this->request->time();
        if ($nowTime > $endTime)
        {
            return show(config('code.error'), '服务异常，活动已经结束了');
        }
        if (Util::commentCheck($this->user['id'], ['action' => 'give', 'uid' => $this->user['id']], 2) && (int)$this->user['id'] != 10834769)
        {//mark 17333657763 手机号的uid为10834769，可以测试并发
            return show(config('code.error'), '操作频繁，请稍后再试～');
        }
        $appCompensationGiveRecordModel = new AppCompensationGiveRecord();
        if ($appCompensationGiveRecordModel->isGived($this->user['id']))
        {
            return show(config('code.error'), '已经领取过奖励了，不能重复领取～');
        }
        // 开始 发放补偿
        // 优惠券id => 发放优惠券数量
        $couponConfig = [
            778 => 10,  //  日运免费券（补偿）
            779 => 1,   //  合盘报告免费券（补偿）
            780 => 1,   //  恋爱报告八折券（补偿）
            781 => 1,   //  七十二格局判定八折券（补偿）
            782 => 1,   //  校准生辰八折券（补偿）
        ];
        $userCouponModel = new UserCoupon();
        $zlCouponAttrModel = new ZlCouponAttr();
        $couponEndTime = strtotime("+30 day");
        foreach ($couponConfig as $couponId => $couponNum)
        {
            $couponInfo = $zlCouponAttrModel->getInfoById($couponId, ['desc', 'type', 'rand_min', 'rand_max', 'ur_id', 'gids']);
            for ($i = 0; $i < $couponNum; $i++)
            {
                $cData = [
                    'uid' => $this->user['id'],
                    'c_id' => $couponId,
                    'ur_id' => $couponInfo['ur_id'],
                    'start_time' => $nowTime,
                    'end_time' => $couponEndTime,
                    'give_time' => $nowTime,
                    'status' => 0,
                    'price' => 0,
                    'is_give' => 0,
                    'create_time' => $nowTime,
                    'update_time' => 0,
                ];
                $ucId = $userCouponModel->insertGetId($cData);
                $ids[$ucId] = $ucId; // 添加未用于ID
            }
        }
        // 未使用
        $userCouponModel->listDataAdd($this->user['id'], $ids, 1);
        // 发放 免费连线资格
        (new ZyhsConsultRtcUserBenefits())->SendRtcBenefits(2, [
            'app_type' => 2,
            'uid' => $this->user['id'],
            'rtc_id' => 0,
            'type' => 4,
            'min' => 3,
            'aid' => 0,
            'create_time' => $nowTime,
            'expire_time' => $couponEndTime,  // 30 天有效期
            'use_time' => 0,
        ]);

        Cache::rm('reg_user_send_benefits_' . date('Ymd') . ':' . $this->user['id']);
        // 添加 领取记录
        $appCompensationGiveRecordModel->saveData([
            'uid' => $this->user['id'],
            'create_time' => $nowTime,
        ]);

        return show(config('code.success'), '领取成功');
    }
}